﻿using System;
using System.Collections.Generic;

namespace Program18
{
    class Program
    {
        static void Main(string[] args)
        {
            //用方法实现：1~100之间的质数及和
            //质数就是只能被1和本身整除的整数
            //2,3,5,7,11,13,17,19,23,29,31,37,41,43...等等25个
            //while (true)
            //{
            //    Console.WriteLine("请输入一个整数:");
            //    int n = Convert.ToInt32(Console.ReadLine());
            //    bool b = IsPrime(n);
            //    Console.WriteLine(b ? "质数" : "非质数");
            //}

            Console.WriteLine("请输入一个范围:");
            int n = Convert.ToInt32(Console.ReadLine());
            var list = new List<int>();
            var sum = GetPrimeSum(n, out list);
            foreach (var item in list)
            {
                Console.Write("{0}  ", item);
            }
            Console.WriteLine("\r\n总和:{0}", sum);
        }
        /// <summary>
        /// 判断是否为质数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        /// <summary>
        /// 判断是否为质数
        /// </summary>
        public static bool IsPrime(int n)
        {
            //1不算质数
            if (n < 2)
                return false;
            else
            // >=2情况
            {
                for (var i = 2; i <= n / 2; i++)
                {
                    if (n % i == 0)
                        return false;
                }

                return true;
            }
        }

       
        /// <returns></returns>
        /// <summary>
        /// 求N以内质数和
        /// </summary>
        public static int GetPrimeSum(int N, out List<int> list)
        {
            list = new List<int>();
            int sum = 0;
            for (var i = 2; i <= N; i++)
            {
                if (IsPrime(i))
                {
                    list.Add(i);
                    sum += i;
                }
            }
            return sum;
        }
    }
}
